var lists = JSON.parse(localStorage.getItem("lists"));
console.log(lists)
if(lists.status == 1){
    $('.check').attr("checked",true)
    console.log($('.check')[0].checked)
}


if(lists.id == ''){
    console.log(1)
}

var add = new Vue({
    el:'#address',
    data:{
        id:null,
        alldizhi:'',
        formdata:{
            name: '',
            tel: '',
            province: '',
            city: '',
            area: '',
            status:0,
            address: ''
        }
    },
    methods: {
        handeladdress(e){
            // e.preventDefault()
            console.log($('.check')[0].checked)
            if($('.check')[0].checked){
                this.formdata.status = 1
            }else{
                this.formdata.status = 0
            }

            if(lists.maxaddress == ''){
                var maxlen = $('#sel_city')[0].innerHTML
                var minlen = maxlen.split("-")
                this.formdata.province = minlen[0] + '省'
                this.formdata.city = minlen[1] + '市'
                this.formdata.area = minlen[2] + " "
            }else{
                var maxlen = $('#sel_city')[0].innerHTML
                var minlen = maxlen.split("-")
                this.formdata.province = minlen[0] 
                this.formdata.city = minlen[1]
                this.formdata.area = minlen[2] 
                console.log(this.formdata.province)
            }

            if (!this.formdata.tel || !(/^1[34578]\d{9}$/.test(this.formdata.tel))) {
                alert('电话无效')
                return false
            }
            const data = new URLSearchParams()
            for (let key in this.formdata) {
                // console.log(key)
                console.log(this.formdata[key])
                data.append(key, this.formdata[key])
            }
            // console.log(this.formdata)
            if(lists.id == ''){
                data.append('address_id', '')
            }else{
                data.append('address_id', lists.id)
            }
            axios.post('user.php?act=update_or_insert_address&ajax=true',data).then(e =>{
                if(e.data.code == 1){
                    alert('请求成功')
                    window.location.href = '/m/user.php?act=account'
                }
            })
        }
    },
    created() {
        this.formdata.name = lists.name
        this.formdata.tel = lists.tel
        this.formdata.province = lists.province
        this.formdata.city = lists.city
        this.formdata.area = lists.area
        this.formdata.address = lists.address
        this.formdata.status =lists.status
        $('#sel_city')[0].innerHTML = lists.maxaddress
    },
})

